import 'package:flutter/cupertino.dart';

/// FileName hide_keyboard_gesture
///
/// @Author lax
/// @Date 2023/4/3
///
/// @Description

class HideKeyboardGesture extends StatelessWidget {
  final HitTestBehavior behavior;

  final bool needHide;

  final Widget? child;

  final Function(double keyboardHeight)? onTap;

  const HideKeyboardGesture({
    super.key,
    this.onTap,
    this.behavior = HitTestBehavior.opaque,
    this.needHide = true,
    this.child,
  });

  @override
  Widget build(BuildContext context) {
    if (needHide) {
      return GestureDetector(
        behavior: behavior, //要设置behavior属性，不然可能点击无效
        onTap: () {
          FocusScope.of(context).unfocus();
          onTap?.call(MediaQuery.of(context).viewInsets.bottom);
        },
        child: child,
      );
    }
    return child ?? const SizedBox();
  }

  static void tap(BuildContext context) {
    // 收起键盘
    FocusScope.of(context).unfocus();
  }
}
